package so.sao.code.service.infrastructure.context.strategy;

/**
 * Created by Administrator on 2017/10/13.
 */
public class TaskRetryStrategy {
    private int retryPauseInMillis = 50;
    private int maxRetryTimes = 3;
    private int maxRetryPauseInMillis = 1000;

    public int getMaxRetryTimes() {
        return maxRetryTimes;
    }

    public boolean shouldRetry(String action, Throwable ex, int retries) {
        if (retries > maxRetryTimes) {
            return false;
        }
        return true;
    }

    /***
     * action  操作名
     * ex      异常
     * retries 第几次重试
     */

    public long getPauseDelay(String action, Throwable ex, int retries) {
        int scale = retryPauseInMillis;
        long delay = (long) Math.pow(2, retries) * scale;
        return Math.min(delay, maxRetryPauseInMillis);
    }
}
